//
// Created by zbtg on 2020/6/9.
//

#include "ros2_chain_node.h"
#include <unistd.h>
#include <sys/types.h>
#include <unistd.h>


int main(int argc, char * argv[])
{

    pid_t pid;

    pid = fork();

    if(pid < 0){
        std::cout << "failed to fork." << std::endl;
        return -1;
    }

    if(pid > 0) // parent
    {
        rclcpp::init(argc, argv);
        auto node = std::make_shared<ChainNodePublisher>();

        node->create_publisher_node("topic");

        rclcpp::WallRate  loop_rate(500ms);

        while (rclcpp::ok()){

            node->chain_node_publish("this is a test.");
            rclcpp::spin_some(node);
            loop_rate.sleep();
        }
        rclcpp::shutdown();

    } else{  //child
        rclcpp::init(argc, argv);
        auto node = std::make_shared<ChainNodeSubsriber>();

        node->create_subscriber_node("topic");

        rclcpp::spin(node);

        std::cout << "after spin." << std::endl;

        rclcpp::shutdown();
    }


    return 0;
}